var beforeBreakfastList = []
var afterBreakfastList = []
var beforeLunchList = []
var afterLunchList = []
var beforeDinnerList = []
var afterDinnerList = []
var beforeBreakfastTimeList = []
var afterBreakfastTimeList = []      
var beforeLunchTimeList = []
var afterLunchTimeList = []
var beforeDinnerTimeList = []
var afterDinnerTimeList = []
var isData = false
var startTime
var endTime
var arrList = []
var index

$(document).ready(function () {
    GetRequestData(GetRequest())
    $("#chartBtnGroup button:first").addClass("active");
});

function GetRequestData(list){
    if(JSON.stringify(list) != "{}"){
        // console.log(list['id'])
        $("#searchInput").val(list['id']);
        getData(list['id'])
    }
}

function getData(id){
    startTime = $('#start').val() != ''? $('#start').val():'2019-01-01';//开始时间
    endTime = $('#end').val() != ''? $('#end').val(): laydate.now(1, 'YYYY-MM-DD');//结束时间
    $.ajax({
        url: global.apiUrl + '/admin_get_user_xuetang_one',
        type: 'GET',
        dataType: 'json',
        data: {
            start_time: startTime,
            end_time: endTime,
            userid: id,
        },
        header: {
            'content-type': 'application/json' //默认值
        },
        success: function(res){
            // console.log(res)
            getEchartsData(res)
            
        }
    })
}

function getEchartsData(list){
    if(list.length!=0){
        beforeBreakfastList = []
        afterBreakfastList = []
        beforeLunchList = []
        afterLunchList = []
        beforeDinnerList = []
        afterDinnerList = []
        beforeBreakfastTimeList = []
        afterBreakfastTimeList = []      
        beforeLunchTimeList = []
        afterLunchTimeList = []
        beforeDinnerTimeList = []
        afterDinnerTimeList = []
        for(let i in list.one){
            beforeBreakfastList.push(list.one[i].blood)
            beforeBreakfastTimeList.push(list.one[i].date)
        }
        for(let i in list.two){
            afterBreakfastList.push(list.two[i].blood)
            afterBreakfastTimeList.push(list.two[i].date)
        }
        for(let i in list.three){
            beforeLunchList.push(list.three[i].blood)
            beforeLunchTimeList.push(list.three[i].date)
        }
        for(let i in list.four){
            afterLunchList.push(list.four[i].blood)
            afterLunchTimeList.push(list.four[i].date)
        }
        for(let i in list.five){
            beforeDinnerList.push(list.five[i].blood)
            beforeDinnerTimeList.push(list.five[i].date)
        }
        for(let i in list.six){
            afterDinnerList.push(list.six[i].blood)
            afterDinnerTimeList.push(list.six[i].date)
        }
        isData = true
        arrList = list
        $("#chartBtnGroup button:first").click().addClass("active");
    }else{
        alert('无测量数据')
        isData = false
    }
}

function showChart(name){
    // console.log(name)
    var title = ''
    var list = []
    var timeList = []
    switch (name) {
        case 'beforBreak':
            title = '早餐前';
            list = beforeBreakfastList;
            timeList = beforeBreakfastTimeList;
            index = 1
            break;
        case 'afterBreak':
            title = '早餐后';
            list = afterBreakfastList;
            timeList = afterBreakfastTimeList;
            index = 2
            break;
        case 'beforLunch':
            title = '午餐前';
            list = beforeLunchList;
            timeList = beforeLunchTimeList;
            index = 3
            break;
        case 'afterLunch':
            title = '午餐后';
            list = afterLunchList;
            timeList = afterLunchTimeList;
            index = 4
            break;
        case 'beforDinner':
            title = '晚餐前';
            list = beforeDinnerList;
            timeList = beforeDinnerTimeList;
            index = 5
            break;
        case 'afterDinner':
            title = '晚餐后';
            list = afterDinnerList;
            timeList = afterDinnerTimeList;
            index = 6
            break;
    }
    if(isData == true){
        if(list.length != 0){
            $("#chartBtnGroup").children().removeClass("active");
            $("#chartBtnGroup .chartBtn").eq(index-1).addClass("active");
            setEcharts(title,list,timeList)
        }else{
            alert('暂无测量数据')
        }
    }else{
        alert('暂无测量数据')
    }
}

function clickBtn(){
    if($("#searchInput").val()!= ''){
        getData($("#searchInput").val())
    }else{
        alert('请输入用户ID')
    }
}

function setEcharts(title,list,timeList) {
    var lineChart = echarts.init(document.getElementById("echarts-line-chart"));
    var lineoption = {
        title : {
            text: title + '变化趋势',
            subtext: startTime + '~' + endTime,
            textAlign: 'center',
            x: 'center'
        },
        tooltip : {
            trigger: 'axis',
            axisPointer : {
                type : 'line',
                lineStyle : {
                    color: '#bbb',
                    width: 1,
                    type: 'solid'
                },
            },
        },
        grid:{
            x:60,
            x2:60,
            y: 80,
            y2:24
        },
        // calculable : true,
        dataZoom:[
            {
              show: false,
              type:'slider',
              start: dataZoom(timeList),
              end: 100,
              left:'3%',
              right:'3%',
              maxValueSpan: 10,
            },
            {
              type: 'inside',
            },
        ],
        xAxis : [
            {
                show: true,
                type : 'category',
                boundaryGap : false,
                data : timeList,
                axisLine:{
                  show: true,
                  lineStyle:{
                    width:1,
                    color: 'black'
                  }
                },
                axisTick:{
                  show:true
                },
                splitLine:{
                  show: false,
                },
                axisLabel:{
                  show:true,
                  interval: 0,
                  formatter: '{value}',
                  formatter:function(value){
                    // return util.setTime13(value) + '日'
                    return value
                  }
                },
            }
        ],
        yAxis : [
            {
                type : 'value',
                show:true,
                axisLine:{
                  show: true,
                  lineStyle:{
                      width:1,
                      color: 'black'
                  }
                },
                axisTick: {
                  show: true,
                },
                splitLine:{
                  show: true,
                },
                axisLabel:{
                  show: true,
                  inside: false,
                  verticalAlign: 'middle',
                  color: 'black',
                  formatter:function(value,index){
                    return value
                  }
                },
            }
        ],
        
        series : [
            {
                name: title,
                type:'line',
                smooth: true,
                symbol: 'circle',
                symbolSize: 6,
                itemStyle : {
                  normal: {
                    color: 'rgb(26, 179, 148)',
                  },
                },
                emphasis:{
                    label:{
                      show: true,
                      position: 'top',
                      color: 'black',
                    },
                    itemStyle:{
                      borderWidth: 12,
                      borderType:'solid',
                      borderColor: 'rgba(26, 179, 148, 0.5)'
                    }
                },
                data: list
            },
        ]
    };
    lineChart.setOption(lineoption);
    $(window).resize(lineChart.resize);
}